package com.example.demo.OtherCoding;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: sfx
 * Date: 2023-05-18
 * Time: 18:17
 */
public class Code_minFallingPathSum {
    class Solution {
        public int minFallingPathSum(int[][] dp) {
            int n = dp.length, res = Integer.MAX_VALUE;

            for (int i = 1; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    if (j == 0) {
                        dp[i][j] += Math.min(dp[i - 1][j], dp[i - 1][j + 1]);
                    } else if (j == n - 1) {
                        dp[i][j] += Math.min(dp[i - 1][j - 1], dp[i - 1][j]);
                    } else {
                        dp[i][j] += Math.min(dp[i - 1][j - 1], Math.min(dp[i - 1][j], dp[i - 1][j + 1]));
                    }
                }
            }
            for (int j = 0; j < n; j++) {
                res = Math.min(res, dp[n - 1][j]);
            }
            return res;
        }
    }

}
